package com.fulihui.information.service;

import com.fulihui.information.api.DislikeService;
import com.fulihui.information.common.ESortType;
import com.fulihui.information.core.repository.DislikeRepository;
import com.fulihui.information.dto.DislikeInfo;
import com.fulihui.information.dto.DislikeStat;
import com.fulihui.information.enums.EDislikeSortFields;
import com.fulihui.information.enums.EDislikeStatus;
import com.fulihui.information.enums.EDislikeType;
import com.fulihui.information.request.*;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.near.toolkit.common.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.near.servicesupport.result.ResultBuilder.fail;
import static org.near.servicesupport.result.ResultBuilder.succ;

/**
 *
 * Created by LeeSon on 2017/1/10 0010.
 */
@Component("dislikeService")
public class DislikeServiceImpl implements DislikeService {
    @Autowired
    private DislikeRepository dislikeRepository;

    @Override
    public BaseResult dislike(DislikeAddRequest request) {
        ServiceAssert.isTrue(EnumUtil.inEnum(request.getType(), EDislikeType.class),
            Errors.Commons.REQUEST_PARAMETER_ERROR);

        if (request.getType().equals(EDislikeType.OTHER.getCode())) {
            ServiceAssert.isTrue(isNotBlank(request.getComment()),
                Errors.Commons.REQUEST_PARAMETER_ERROR);
        }

        if (dislikeRepository.isExist(request.getUserId(), request.getContentId())) {
            return fail(102, "谢谢，您已经反馈过此信息！");
        }

        boolean addDislike = dislikeRepository.addDislike(request);
        ServiceAssert.isTrue(addDislike, Errors.Commons.SYSTEM_ERROR);

        return succ();
    }

    @Override
    public BaseResult modifyStatus(DislikeStatusRequest request) {
        ServiceAssert.isTrue(EnumUtil.inEnum(request.getStatus(), EDislikeStatus.class),
            Errors.Commons.REQUEST_PARAMETER_ERROR);

        boolean modifyStatus = dislikeRepository.modifyStatus(request.getDislikeId(),
            request.getStatus(), request.getOperator());

        ServiceAssert.isTrue(modifyStatus, Errors.Commons.SYSTEM_ERROR);

        return succ();
    }

    @Override
    public TPageResult<DislikeInfo> queryPage(DislikeQueryRequest request) {
        String status = request.getStatus();
        if (isNotEmpty(status)) {
            ServiceAssert.isTrue(EnumUtil.inEnum(status, EDislikeStatus.class),
                Errors.Commons.REQUEST_PARAMETER_ERROR);
        }

        String type = request.getType();
        if (isNotEmpty(type)) {
            ServiceAssert.isTrue(EnumUtil.inEnum(type, EDislikeType.class),
                Errors.Commons.REQUEST_PARAMETER_ERROR);
        }

        String sortType = request.getSortType();
        if (isNotEmpty(sortType)) {
            ServiceAssert.isTrue(EnumUtil.inEnum(sortType, ESortType.class),
                Errors.Commons.REQUEST_PARAMETER_ERROR);
        }

        String sortField = request.getSortFiled();
        if (isNotEmpty(sortField)) {
            ServiceAssert.isTrue(EnumUtil.inEnum(sortField, EDislikeSortFields.class),
                Errors.Commons.REQUEST_PARAMETER_ERROR);
        }

        List<DislikeInfo> dislikeInfoList = dislikeRepository.queryPage(request);
        int queryPageCount = dislikeRepository.queryPageCount(request);
        return ResultBuilder.succTPage(dislikeInfoList, request.getPage(), request.getRows(),
            queryPageCount);
    }

    @Override
    public TSingleResult<DislikeInfo> detail(DislikeDetailRequest request) {
        return ResultBuilder.succTSingle(dislikeRepository.detail(request.getDislikeId(), true));
    }

    @Override
    public TSingleResult<DislikeStat> stat(DislikeStatRequest request) {
        return ResultBuilder.succTSingle(dislikeRepository.stat());
    }
}